import UsersApi from "../api/admin.js";  

export const request = ({ type = "GET", url, data = {}, headers = {} }) =>
    new Promise(resolve => {
        $.ajax({
            type,
            url,
            data,
            headers,
            success: resolve,   //resolve（data）携带服务器返回的数据
        })
    })

// 每次请求都要鞋带上token值
export const ajaxInit = () => {
    $.ajaxSetup({
        beforeSend(xhr) {
            const token = UsersApi.getToken();
            if (token) {
                // 固定的书写格式  bearer必须有一个空格
                // 设置一个请求头，和一个请求头的值
                xhr.setRequestHeader("authorization", `Bearer ${token}`)
            }
        }
    })
}
